
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.Authorization;
//using Core.SimpleTemp.Common.Core.Repository.Internal.Data;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.RepositoryEntityFrameworkCore.Sys;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
namespace Core.SimpleTemp.Mvc.Api.Sys

{
    /// <summary>
    /// SysDuty
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]
    public class SysDutyController : CoreApiController
    {
        private readonly SysDutyService _service;
        private readonly SysDepartmentDutyService _ddService;
        private readonly SysDepartmentDutyRepository _ddRepository;
        /// <summary>
        /// SysDuty
        ///</summary>
        public SysDutyController(SysDutyService service, SysDepartmentDutyService ddService, SysDepartmentDutyRepository ddRepository
        )
        {
            _service = service;
            _ddService = ddService;
            this._ddRepository = ddRepository;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<SysDutyDto> pagingQueryModel)
        {
            IPageModel<SysDutyDto> result;
            var lpom = _service.GetLoadPageOffsetModelAuto(pagingQueryModel);
            lpom.Order = x => x.Position;
            lpom.QueryWhere = this.CreateQRList().Add(nameof(SysDutyDto.Name), "in").Add(nameof(SysDutyDto.CreateFactory), "=").Add("SysDepartmentID", "=");
            lpom.Selector = u => new SysDuty() { Id = u.Id, Name = u.Name, IsLeader = u.IsLeader, Position = u.Position, CreateFactory = u.CreateFactory, SysDepartmentID = u.SysDepartmentID };
            result = await _service.LoadPageOffsetAsync(lpom);
            return this.JsonSuccess(result);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetListExtend")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.GetListExtend), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetListExtend()
        {
            PagingModel<SysDutyDto> pagingQueryModel = new PagingModel<SysDutyDto>
            {
                Limit = 9999,
                Offset = 0,
                Model = new SysDutyDto()
            };
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            var result= await this.GetList(pagingQueryModel);
            return result;
        }

        [HttpPost("GetAllList")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.GetAllList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetAllList(PagingModel<SysDutyDto> pagingQueryModel)
        {

            pagingQueryModel.Limit = 99999;
            IPageModel<SysDutyDto> result;
            result = await _service.GetList(pagingQueryModel);

            List<object> l = new List<object>();
            foreach (SysDutyDto p in result.PageData)
            {
                object o = new { id = p.Id, pid = 0, name = p.Name, code = p.Id };
                l.Add(o);
            }

            return this.JsonSuccess(l);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetListByDept")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.GetListByDept), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetListByDept(PagingModel<SysDutyDto> pagingQueryModel)
        {
            IPageModel<SysDutyDto> result;
            var lpom = _service.GetLoadPageOffsetModelAuto(pagingQueryModel);
            lpom.Order = x => x.Position;
            lpom.QueryWhere = this.CreateQRList()
                .Add(nameof(SysDutyDto.SysDepartmentID), "=")
                .Add(nameof(SysDutyDto.CreateFactory), "=")
                .Add(nameof(SysDutyDto.IsLeader), "=")
                ;
            lpom.Selector = u => new SysDuty()
            {
                Id = u.Id,
                Name = u.Name,
                IsLeader = u.IsLeader,
                Position = u.Position,
                CreateFactory = u.CreateFactory,
                SysDepartmentID = u.SysDepartmentID
            };
            result = await _service.LoadPageOffsetAsync(lpom);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>  SaveAsync(SysDutyDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                try
                {
                    var result = await _service.InsertAsync(dto);
                    var departmentDuty = new SysDepartmentDutyDto
                    {
                        Id = Guid.NewGuid(),
                        SysDepartmentID = (Guid)result.SysDepartmentID,
                        SysDutyID = result.Id,

                    };
                    var result2 = _ddService.InsertAsync(departmentDuty);
                }
                catch (Exception ex)
                {
                    int x = 0;
                }

                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                var ddrecord = _ddRepository._dbContext.SysDepartmentDuty.FirstOrDefault(x => x.SysDutyID == dto.Id);
                if (ddrecord != null)
                {
                    ddrecord.SysDepartmentID = (Guid)dto.SysDepartmentID;
                    _ddRepository._dbContext.SaveChanges();
                }
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {
            string message = "";
            var checkResult = _service.DutyDeleteCheck(ids, out message);
            if (checkResult)
            {
                await _service.DeleteBatchAsync(ids);
                return this.JsonSuccess();
            }
            else
            {
                return this.JsonFaild(message);
            }
            

        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        [PermissionFilter(nameof(SysDutyController) + "_" + nameof(SysDutyController.Get), AuthorizationConst.GET)]
        public async Task<IActionResult>  Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model);
        }


        [HttpGet("GetFactory")]
        public async Task<IActionResult> GetFactory(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model.CreateFactory);
        }

        #endregion


    }
}
